import sys

from PyQt5.QtWidgets import QMainWindow, QApplication

from generate_id import gen_id_num
from qt_design.mainWindow import Ui_MainWindow
from datas.chinese_admini_divisions_dict import ProvinceCode, CitiesCode
from datas.chinses_area import Area


class MyWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.setupUi(self)

        # 初始化显示省、市、区
        self.comboBox_province.addItems(get_provinces())
        self.update_comboBox_city()
        self.update_comboBox_districts()

        # 初始化性别选项
        self.set_gender()
        # 初始化生成数量选项
        self.set_num()

        # 省 下拉索引发生改变时发射信号触发绑定的事件，更新 市 的下拉内容
        # Activated             当用户选中一个下拉选项时发射该信号
        # currentIndexChanged   当下拉选项的索引发生改变时发射该信号
        # highlighted           当选中一个已经选中的下拉选项时，发射该信号
        self.comboBox_province.activated.connect(self.update_comboBox_city)

        # 市 下拉索引发生改变时发射信号触发绑定的事件，更新 区 的下拉内容
        self.comboBox_city.currentIndexChanged.connect(self.update_comboBox_districts)

        # 点击“生成”操作
        self.pushButton_generate.clicked.connect(self.generate)

    def update_comboBox_city(self):
        """
        获取省，更新市
        :return:
        """
        province = self.comboBox_province.currentText()
        code = get_province_code_by_name(province)
        cities_list = get_cities_by_province_code(code)
        # 清空
        self.comboBox_city.clear()
        self.comboBox_district.clear()
        if cities_list is not None:
            # 添加内容
            self.comboBox_city.addItems(cities_list)

    def update_comboBox_districts(self):
        """
        获取市，更新区
        :return:
        """
        city = self.comboBox_city.currentText()
        code = get_city_code_by_name(city)
        districts_list = get_districts_by_city_code(code)
        self.comboBox_district.clear()
        if districts_list is not None:
            self.comboBox_district.addItems(districts_list)

    def generate(self):
        """
        生成身份证号码，在文本框中显示
        :return:
        """
        # 获取地区的行政规划代码
        area = self.comboBox_district.currentText()
        area_code = ""
        if len(area) != 0:
            area_code = get_district_code_by_name(area)

        # 生成按钮，获取日期
        date = self.dateEdit_birthday.date()
        # YYYY-MM-DD格式
        date = date.toPyDate()
        date = str(date).replace("-", "")

        # 获取性别
        gender = self.comboBox_gender.currentText()
        if gender == '男':
            gender_code = 1
        else:
            gender_code = 0

        # 获取生成数量
        num = self.comboBox_num.currentText()
        num = int(num)

        id_num_list = []
        if len(area_code) != 0:
            while num != 0:
                id_num = gen_id_num(area_code, date, gender_code)
                id_num_list.append(id_num)
                num -= 1

            id_num_list_str = ''
            for item in id_num_list:
                id_num_list_str = id_num_list_str + item + '\n'
            self.textBrowser_id_num.setText(id_num_list_str)
        else:
            self.textBrowser_id_num.setText("暂不支持该地区")

    def set_gender(self):
        """
        性别
        :return:
        """
        gender = {'男': '1', '女': '0'}
        self.comboBox_gender.addItems(gender)

    def set_num(self):
        """
        数量
        :return:
        """
        num_dict = {'1': '1', '5': '5', '10': '10'}
        self.comboBox_num.addItems(num_dict)
        # self.comboBox_num.get


def get_provinces():
    """
    获取所有的省、直辖市、自治区
    :return: 中国所有的省、直辖市、自治区
    """
    procinces_list = []
    for key, value in ProvinceCode.items():
        procinces_list.append(key)
    return procinces_list


def get_cities_by_province_code(code):
    """
    根据省编码获得城市下所有的城市的名称
    :param code: 省编码
    :return: 省内所有的城市的名称
    """
    code = str(code)
    cities_list = []
    special_administrative_region = ['810000', '820000', '710000']
    if code in special_administrative_region:  # 特别行政区、台湾省，返回空
        return
    else:
        for key, value in CitiesCode.items():
            if value[0: 2] == code[0: 2]:
                cities_list.append(key)
    return cities_list


def get_districts_by_city_code(code):
    """
    根据城市编码获得城市下所有的区的名称
    :param code: 城市编码
    :return: 城市下所有的区的名称
    """
    code = str(code)
    districts_list = []
    # 直辖市
    municipality = ['110000', '120000', '310000', '500000']
    if len(code) != 0:
        if code in municipality:
            for key, value in Area.items():
                if value[0: 2] == code[0: 2] and value != code:
                    districts_list.append(key)
        else:
            for key, value in Area.items():
                if value[0: 4] == code[0: 4] and value != code:
                    districts_list.append(key)
        return districts_list
    else:
        return  # 特别行政区、台湾省，返回空


def get_province_code_by_name(name):
    """
    根据省名称获得编码
    :param name: 省名称
    :return: 编码
    """
    for key, value in ProvinceCode.items():
        if key == name:
            return value


def get_city_code_by_name(name):
    """
    根据城市名称获得编码
    :param name: 城市名称
    :return: 编码
    """
    for key, value in CitiesCode.items():
        if key == name:
            return value


def get_district_code_by_name(name):
    """
    根据区名称获得编码
    :param name: 区名称
    :return: 编码
    """
    for key, value in Area.items():
        if key == name:
            return value


if __name__ == '__main__':
    app = QApplication(sys.argv)
    # 主窗口
    myWin = MyWindow()
    myWin.show()
    sys.exit(app.exec_())
